import $ from 'pages/common';
import amapFile from 'libs/amap-wx.js';
App({
  onLaunch(options) {
    this.setIphoneX();
    this.getnavinfo()
  },
  setIphoneX() {
    let _self = this;
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (res.safeArea.top > 20) { //x及以上的异形屏top为44，非异形屏为20
          _self.globalData.isIphoneX = true;
        } else {
          _self.globalData.isIphoneX = false;
        }
        wx.setStorageSync('modelmes', modelmes)
      },
    })
  },
  onShow(options) {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: '版本提示',
            content: '新版本已经下载好，开始体验吧！',
            showCancel: false,
            success(res) {
              //新版本重启
              updateManager.applyUpdate()
            }
          })
        })
        updateManager.onUpdateFailed(function () {
          //新版本下载失败
        })
      }
    })
    if (options.referrerInfo && options.referrerInfo.extraData) {
      this.globalData.extraData = options.referrerInfo.extraData
    }
  },
  getUserInfo(cb) {
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success() {
          wx.getUserInfo({
            success(res) {
              this.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(this.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  getnavinfo() {
    let systemInfo = wx.getSystemInfoSync();
    let pxToRpxScale = 750 / systemInfo.windowWidth; //px转换到rpx的比例
    let statuHeight = systemInfo.statusBarHeight * pxToRpxScale; //状态栏的高度
    let titleHeight = 44 * pxToRpxScale; //导航栏高度，这个一般是固定的
    this.globalData.statuHeight = statuHeight
    this.globalData.titleHeight = titleHeight
  },
  globalData: {
    extraData: {},
    getLocation: {},
    isIphoneX: false,
    getLocationFn: (callback) => {
      let Amap = new amapFile.AMapWX({
        key: '6feea372f82a1b2dd4b9d5c766304c92'
      });
      Amap.getRegeo({
        success: (res) => {
          let data = res[0]
          let globalData = $.gData()
          globalData.getLocation = data
          let regeocodeData = data.regeocodeData
          let address = regeocodeData.addressComponent
          let f_address = regeocodeData.formatted_address
          let area_text = address.province + address.city + address.district
          globalData.area = {
            text: area_text,
            code: address.adcode
          }
          globalData.address = f_address.split(area_text)[1] || data.name
          callback && callback()
        },
        fail: (info) => {
          console.log(info)
        }
      })
    },
    area: {
      text: "",
      code: ""
    },
    ins_uuid: null, // 最后操作的学校institution_uuid
    campusData: null, // 校区列表
    isGuide: 'NO',
    insList: []
  }
})